<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-ad">
        <h2 class="ad-title">欢迎来到 权限管理系统</h2>
        <p class="ad-description">一个根据身份来赋予权限的系统</p>
        <p></p>
      </div>
      <div class="login-form">
        <h2 class="login-title">登录 权限系统</h2>
        <el-form
          @submit.prevent="handleLogin"
          :model="form"
          :rules="rules"
          ref="loginForm"
          label-width="80px"
          label-position="top"
        >
          <el-form-item label="用户名" prop="account">
            <el-input v-model="form.account" placeholder="请输入用户名" />
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input
              type="password"
              v-model="form.password"
              placeholder="请输入密码"
              show-password
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" native-type="submit" style="width: 100%"
              >登录</el-button
            >
          </el-form-item>
        </el-form>
        <div class="login-links">
          <el-link type="primary" href="#">忘记密码？</el-link>
          <el-link type="primary" href="#" style="margin-left: 10px"
            >注册账号</el-link
          >
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from "vue";
import { useUserStore } from "@/stores/model/user";
import { useRouter } from "vue-router";
import routerInstance from "@/router";

const router = useRouter();

const userStore = useUserStore();

const form = ref({
  account: "",
  password: "",
});

const loginForm = ref(null);

const rules = ref({
  account: [
    { required: true, message: "请输入用户名", trigger: "blur" },
    { min: 3, max: 15, message: "长度在 3 到 15 个字符", trigger: "blur" },
  ],
  password: [
    { required: true, message: "请输入密码", trigger: "blur" },
    { min: 6, max: 20, message: "长度在 6 到 20 个字符", trigger: "blur" },
  ],
});

const handleLogin = async () => {
  loginForm.value.validate(async (valid) => {
    if (valid) {
      try {
        const res = await userStore.getUserInfo(
          form.value.account,
          form.value.password
        );
        if (res) {
          // const userMenuList = await userStore.getUserMenuTree();
          // userMenuList.forEach((item) => {
          //   routerInstance.addRoute(item);
          // });

          console.log(router.push(`/`));
          // router.push("/settingSystem/settingRouter");
        }
      } catch (error) {
        console.error("登录出错:", error);
      }
    } else {
      console.log("验证失败");
      return false;
    }
  });
};
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f5f5f5;
}

.login-box {
  display: flex;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
  width: 800px;
}

.login-ad {
  flex: 1;
  padding-right: 20px;
  border-right: 1px solid #ebeef5;
  background-color: rgb(64, 158, 255);
  padding: 20px;
}

.ad-title {
  font-size: 24px;
  color: white;
  margin-bottom: 10px;
}

.ad-description {
  font-size: 16px;
  color: white;
}

.login-form {
  flex: 1;
  padding: 20px;
}

.login-title {
  text-align: center;
  margin-bottom: 20px;
  color: #333;
}

.login-links {
  text-align: center;
  margin-top: 15px;
}

.login-links .el-link {
  margin: 0 10px;
}
</style>
